﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Mvc;
using Mvc4WebApiKoTb.Helpers;
using Mvc4WebApiKoTb.Helpers.Enums;
using Mvc4WebApiKoTb.Models;
using Newtonsoft.Json;

namespace Mvc4WebApiKoTb.Controllers
{
    /// <summary>
    /// http://www.west-wind.com/weblog/posts/2012/Aug/21/An-Introduction-to-ASPNET-Web-API
    /// </summary>
    public class PersonEditApiController : ApiController
    {
        /// <summary>
        /// POST api/PersonEditApi/SavePerson
        /// Mit HttpResponseMessage kann man den kompletten Response anpassen, z.b. welches Format der Ausgabestrom hat - Standard ist hier JSON
        /// </summary>
        public HttpResponseMessage SavePerson(PersonModel value)
        {
            if (ModelState.IsValid)
            {
                if (value == null)
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }

                value.Id = 5;
                //Wird automatisch in JSON Objekt umgewandelt und gibt den StatusCode 201 für Created zurück!
                HttpResponseMessage response = Request.CreateResponse<PersonModel>(HttpStatusCode.Created, value);
                response.Headers.Location = new Uri(Request.RequestUri, "/api/PersonEditApi/SavePerson/" + value.Id.ToString());
                return response;     
            }
            
            throw  new HttpResponseException(HttpStatusCode.BadRequest);
        }

        /// <summary>
        /// POST api/PersonEditApi/SavePerson
        /// Mit HttpResponseMessage kann man den kompletten Response anpassen, z.b. welches Format der Ausgabestrom hat - Standard ist hier JSON
        /// </summary>
        public PersonModel Save(PersonModel value)
        {
            if (ModelState.IsValid)
            {
                if (value == null)
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }

                //Gibt den Statuscode 200 für OK zurück!
                value.Id = 5;
                return value;
            }

            throw new HttpResponseException(HttpStatusCode.BadRequest);
        }

        /// <summary>
        /// POST api/PersonEditApi/SaveWithParams?take=10&skip=20
        /// Aufruf der Funktion inkl. URL Parametern
        /// </summary>
        public PersonModel SaveWithParams(PersonModel value, int take, int skip)
        {
            if (ModelState.IsValid)
            {
                if (value == null)
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }

                //Gibt den Statuscode 200 für OK zurück!
                value.Id = 5;
                return value;
            }

            throw new HttpResponseException(HttpStatusCode.BadRequest);
        }
    }
}
